#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QUrl>
#include <QDir>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    mediaPlaylist = new QMediaPlaylist(this);
    mediaPlayer = new QMediaPlayer(this);
    mediaPlayer->setPlaylist(mediaPlaylist);

    connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChange);
    connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChange);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::positionChange(quint64 pos)
{
    qDebug() << "pos:" << pos << endl;

    if (!ui->horizontalSlider->isSliderDown()) {
        ui->horizontalSlider->setValue(static_cast<int>(pos / 1000));
    }
}

void MainWindow::durationChange(quint64 duration)
{
    ui->horizontalSlider->setRange(0, static_cast<int>(duration / 1000));
    qDebug() << "duration" << duration << endl;
}

void MainWindow::on_prev_clicked()
{
    if (mediaPlaylist->mediaCount() == 0) {
        qDebug() << "media conut is zero" << endl;
        return ;
    }
    mediaPlaylist->previous();
    mediaPlayer->play();
}

void MainWindow::on_play_clicked()
{
    auto state = mediaPlayer->state();
    switch (state) {
    case QMediaPlayer::StoppedState: {
        mediaPlayer->play();
        qDebug() << "play" << endl;
    } break;
    case QMediaPlayer::PlayingState: {
        mediaPlayer->pause();
        qDebug() << "pause" << endl;
    } break;
    case QMediaPlayer::PausedState: {
        mediaPlayer->play();
        qDebug() << "play" << endl;
    } break;
    }
}

void MainWindow::on_next_clicked()
{
    if (mediaPlaylist->mediaCount() == 0) {
        qDebug() << "media conut is zero" << endl;
        return ;
    }
    mediaPlaylist->next();
    mediaPlayer->play();
}

void MainWindow::on_lineEdit_editingFinished()
{
    scanMusic(ui->lineEdit->text());
}

void MainWindow::scanMusic(QString dirPath)
{
    QDir dir(dirPath);
    QDir dirbsolutePath(dir.absolutePath());

    if (!dirbsolutePath.exists()) {
        qDebug() << dir << endl;
        return;
    }

    qDebug() << "dir" << dir << endl;
    qDebug() << "dirbsolutePath" << dirbsolutePath << endl;

    ui->listWidget->clear();

    QStringList filter;
    filter << "*.mp3";
    QFileInfoList files = dirbsolutePath.entryInfoList(filter, QDir::Files);

    for(int i = 0; i < files.count(); i++) {
        QString filePath = files.at(i).filePath();
        if (mediaPlaylist->addMedia(QUrl::fromLocalFile(filePath))) {
            ui->listWidget->addItem(files.at(i).fileName());
            qDebug() << "info.fileName" << filePath << endl;
        } else {
            qDebug() << "Error filePath" << filePath << endl;
            qDebug() << mediaPlaylist->errorString().toUtf8().data() << endl;
            qDebug() << "Error number:" << mediaPlaylist->error() << endl;
        }
    }
}

void MainWindow::on_horizontalSlider_2_sliderMoved(int position)
{
    mediaPlayer->setVolume(position);
}
